﻿Imports System
Imports System.Workflow.ComponentModel

Namespace System.Workflow.Runtime
    Friend NotInheritable Class ServiceEnvironment
        Inherits AmbientEnvironment
        ' Methods
        Friend Sub New(ByVal currentActivity As Activity)
            MyBase.New(currentActivity)
            GC.SuppressFinalize(Me)
        End Sub

        Friend Shared Function IsInServiceThread(ByVal instanceId As Guid) As Boolean
            Return ((ServiceEnvironment.WorkflowInstanceId = instanceId) OrElse DebuggerThreadMarker.IsInDebuggerThread)
        End Function


        ' Properties
        Private Shared ReadOnly Property CurrentActivity As Activity
            Get
                Return [TryCast](AmbientEnvironment.Retrieve,Activity)
            End Get
        End Property

        Friend Shared ReadOnly Property QueuingService As WorkflowQueuingService
            Get
                Dim currentActivity As Activity = ServiceEnvironment.CurrentActivity
                Dim executor As WorkflowExecutor = Nothing
                If (Not currentActivity Is Nothing) Then
                    executor = ContextActivityUtils.RetrieveWorkflowExecutor(currentActivity)
                End If
                Do While (Not currentActivity Is Nothing)
                    If (currentActivity Is DirectCast(executor, IWorkflowCoreRuntime).CurrentAtomicActivity) Then
                        Dim properties As TransactionalProperties = DirectCast(currentActivity.GetValue(WorkflowExecutor.TransactionalPropertiesProperty), TransactionalProperties)
                        If ((Not properties Is Nothing) AndAlso (Not properties.LocalQueuingService Is Nothing)) Then
                            Return properties.LocalQueuingService
                        End If
                    End If
                    currentActivity = currentActivity.Parent
                Loop
                Return Nothing
            End Get
        End Property

        Friend Shared ReadOnly Property WorkBatch As IWorkBatch
            Get
                Dim currentActivity As Activity = ServiceEnvironment.CurrentActivity
                If (currentActivity Is Nothing) Then
                    Return Nothing
                End If
                Return DirectCast(currentActivity.GetValue(WorkflowExecutor.TransientBatchProperty), IWorkBatch)
            End Get
        End Property

        Friend Shared ReadOnly Property WorkflowInstanceId As Guid
            Get
                Dim currentActivity As Activity = ServiceEnvironment.CurrentActivity
                If (currentActivity Is Nothing) Then
                    Return Guid.Empty
                End If
                Return DirectCast(ContextActivityUtils.RootContextActivity(currentActivity).GetValue(WorkflowExecutor.WorkflowInstanceIdProperty), Guid)
            End Get
        End Property


        ' Fields
        Friend Shared ReadOnly debuggerThreadGuid As Guid = New Guid("54D747AE-5CC6-4171-95C8-0A8C40443915")
    End Class
End Namespace

